<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="账号" prop="username">
    <el-input v-model="dataForm.username" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
    <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
    <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="avatar">
    <el-input v-model="dataForm.avatar" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
    <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="性别 0女 1男" prop="sex">
    <el-input v-model="dataForm.sex" placeholder="性别 0女 1男"></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="birthday">
    <el-input v-model="dataForm.birthday" placeholder="生日"></el-input>
    </el-form-item>
    <el-form-item label="地址" prop="address">
    <el-input v-model="dataForm.address" placeholder="地址"></el-input>
    </el-form-item>
    <el-form-item label="个人简介" prop="description">
    <el-input v-model="dataForm.description" placeholder="个人简介"></el-input>
    </el-form-item>
    <el-form-item label="是否官方账号 1是 0否" prop="official">
    <el-input v-model="dataForm.official" placeholder="是否官方账号 1是 0否"></el-input>
    </el-form-item>
    <el-form-item label="账号状态 1正常 2注销 3禁用" prop="status">
    <el-input v-model="dataForm.status" placeholder="账号状态 1正常 2注销 3禁用"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
    <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
    <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除  0删除 1未删除" prop="deleted">
    <el-input v-model="dataForm.deleted" placeholder="是否删除  0删除 1未删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          username: '',
          password: '',
          nickname: '',
          avatar: '',
          phone: '',
          sex: '',
          birthday: '',
          address: '',
          description: '',
          official: '',
          status: '',
          createTime: '',
          updateTime: '',
          deleted: ''
        },
        dataRule: {
          username: [
            { required: true, message: '账号不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          avatar: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别 0女 1男不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          address: [
            { required: true, message: '地址不能为空', trigger: 'blur' }
          ],
          description: [
            { required: true, message: '个人简介不能为空', trigger: 'blur' }
          ],
          official: [
            { required: true, message: '是否官方账号 1是 0否不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '账号状态 1正常 2注销 3禁用不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '是否删除  0删除 1未删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/forum/member/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.member.username
                this.dataForm.password = data.member.password
                this.dataForm.nickname = data.member.nickname
                this.dataForm.avatar = data.member.avatar
                this.dataForm.phone = data.member.phone
                this.dataForm.sex = data.member.sex
                this.dataForm.birthday = data.member.birthday
                this.dataForm.address = data.member.address
                this.dataForm.description = data.member.description
                this.dataForm.official = data.member.official
                this.dataForm.status = data.member.status
                this.dataForm.createTime = data.member.createTime
                this.dataForm.updateTime = data.member.updateTime
                this.dataForm.deleted = data.member.deleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/forum/member/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'nickname': this.dataForm.nickname,
                'avatar': this.dataForm.avatar,
                'phone': this.dataForm.phone,
                'sex': this.dataForm.sex,
                'birthday': this.dataForm.birthday,
                'address': this.dataForm.address,
                'description': this.dataForm.description,
                'official': this.dataForm.official,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'deleted': this.dataForm.deleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
